package com.hy.study.practice.queue;

import java.util.Scanner;

/**
 * 循环队列的练习
 */
public class CrirleArrayQueue2 {

    public static void main(String[] args) {
        CircleQueue2 circleQueue = new CircleQueue2(4);
        char type = ' ';
        Scanner scanner = new Scanner(System.in);
        System.out.println("-------请选择---------");
        for (; ; ) {
            System.out.println("a(add) 新增数据\n g(get) 获取数据 \n h(head) 得到头节点 \n e(exit) 退出程序\n s(show) 展示整个数组 ");
            type = scanner.next().charAt(0);
            switch (type) {
                case 'a':
                    System.out.println("请输入一个数:");
                    try {
                        int value = scanner.nextInt();
                        circleQueue.addQueue(value);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'g':
                    try {
                        System.out.println(circleQueue.getQueue());
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 's':
                    try {
                        circleQueue.showQueue();
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        System.out.println(circleQueue.head());
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    System.out.println("程序退出");
                    scanner.close();
                    System.exit(0);
                default:
                    break;

            }
        }

    }


}

class CircleQueue2 {

    private int front;
    private int rear;
    private int[] arr;
    private int maxSize = 16;

    public CircleQueue2(int maxSize) {
        this.maxSize = maxSize;
        arr = new int[maxSize];
        front = 0;
        rear = 0;
    }

    public CircleQueue2() {
        arr = new int[maxSize];
        front = 0;
        rear = 0;
    }

    /**
     * 向队列中添加一个元素
     *
     * @param value
     */
    public void addQueue(int value) {
        if (isFull()) {
            throw new RuntimeException("队列已满，无法添加");
        }
        arr[rear] = value;
        rear = (rear + 1) % maxSize;
    }

    /**
     * 队列中获取元素
     */
    public int getQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空,无法取出数据");
        }
        int value = arr[front];
        front = (front + 1) % maxSize;
        return value;

    }

    /**
     * 得到头元素
     *
     * @return
     */
    public int head() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空,无法取出数据");
        }
        return arr[front];
    }

    /**
     * 展示 队列元素
     */
    public void showQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空,无法取出数据");
        }
        for (int i = front; i < front + size(); i++) {
            System.out.printf("arr[%d]=%d\n", i, arr[i]);
        }

    }

    /**
     * 判断队列是否满
     *
     * @return
     */
    public boolean isFull() {
        return (rear + 1) % maxSize == front;
    }

    /**
     * 判断队列是否为空
     *
     * @return
     */
    public boolean isEmpty() {
        return rear == front;
    }

    /**
     * 获取整个队列的大小
     *
     * @return
     */
    public int size() {
        return (rear + maxSize - front) % maxSize;
    }
}
